package com.ruoyi.project.system.controller;

import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.utils.RandomUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.domain.SysReceive;
import com.ruoyi.project.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysExchange;
import com.ruoyi.project.system.service.ISysExchangeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import test.util.SequenceUtils;

/**
 * 兑换码记录Controller
 *
 * @author theme
 * @date 2021-08-06
 */
@Api(description = "兑换码记录Controller" )
@RestController
@RequestMapping("/system/exchange" )
public class SysExchangeController extends BaseController {
    @Autowired
    private ISysExchangeService sysExchangeService;

    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    private ISysDictDataService sysDictDataService;

/**
 * 查询兑换码记录列表
 */
@ApiOperation("查询兑换码记录列表" )
@PreAuthorize("@ss.hasPermi('system:exchange:list')" )
@GetMapping("/list" )
        public TableDataInfo list(SysExchange sysExchange) {
        startPage();
        List<SysExchange> list = sysExchangeService.selectSysExchangeList(sysExchange);
        return getDataTable(list);
    }

    /**
     * 获取兑换码记录详细信息
     */
    @ApiOperation("获取兑换码记录详细信息" )
    @PreAuthorize("@ss.hasPermi('system:exchange:query')" )
    @GetMapping(value = "/{exchangeId}" )
    public AjaxResult getInfo(@PathVariable("exchangeId" ) Long exchangeId) {
        return AjaxResult.success(sysExchangeService.selectSysExchangeById(exchangeId));
    }

    /**
     * 新增兑换码记录
     */
    @ApiOperation("新增兑换码记录" )
    @PreAuthorize("@ss.hasPermi('system:exchange:add')" )
    @Log(title = "兑换码记录" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysExchange sysExchange) {
        return toAjax(sysExchangeService.insertSysExchange(sysExchange));
    }

    /**
     * 新增兑换码记录
     */
    @ApiOperation("批量生成兑换码记录" )
    @Log(title = "批量生成兑换码记录" , businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batch(@RequestBody SysExchange datas) {
        if(datas.getExchangeList().isEmpty()){
            return AjaxResult.error("参数错误!");
        }
        List<SysExchange> list = new ArrayList<>();
        for(SysExchange sysExchange:datas.getExchangeList()){
            if(null==sysExchange.getDictCode()){
                return AjaxResult.error("参数错误!");
            }
            SysDictData dictData=sysDictDataService.selectDictDataById(sysExchange.getDictCode());
            if(null==dictData){
                return AjaxResult.error("参数错误!");
            }
            sysExchange.setCreateBy(SecurityUtils.getUsername());
            sysExchange.setCreateTime(new Date());
            sysExchange.setUpdateBy(SecurityUtils.getUsername());
            sysExchange.setUpdateTime(new Date());
            sysExchange.setStatus("0");
            sysExchange.setType(dictData.getDictValue());
            sysExchange.setMoney(dictData.getRemark());
            sysExchange.setTypeName(dictData.getDictLabel());
            sysExchange.setCompanyId(sysExchange.getCompanyId());
            sysExchange.setCode("现代商业银行");
            list.add(sysExchange);
        }
        return toAjax(sysExchangeService.insertBatchSysExchange(list));
    }


    /**
     * 修改兑换码记录
     */
    @ApiOperation("修改兑换码记录" )
    @PreAuthorize("@ss.hasPermi('system:exchange:edit')" )
    @Log(title = "兑换码记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysExchange sysExchange) {
        return toAjax(sysExchangeService.updateSysExchange(sysExchange));
    }

    /**
     * 删除兑换码记录
     */
    @ApiOperation("删除兑换码记录" )
    @PreAuthorize("@ss.hasPermi('system:exchange:remove')" )
    @Log(title = "兑换码记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{exchangeIds}" )
    public AjaxResult remove(@PathVariable Long[] exchangeIds) {
        return toAjax(sysExchangeService.deleteSysExchangeByIds(exchangeIds));
    }

    @GetMapping("/export")
    public AjaxResult export(SysExchange sysExchange) {
        List<SysExchange> list = sysExchangeService.selectSysExchangeList(sysExchange);
        ExcelUtil<SysExchange> util = new ExcelUtil<SysExchange>(SysExchange.class);
        return util.exportExcel(list, "领取数据");
    }
}
